package com.dxy.lc.socketio.handler;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.dxy.lc.base.AppConstant;
import com.dxy.lc.model.bean.MsgPayload;
import com.dxy.lc.service.MessageService;
import com.dxy.lc.socketio.OnMessageEventHandler;
import com.dxy.lc.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LcOnMessageEventHandler implements OnMessageEventHandler<MsgPayload> {

    @Autowired
    private MessageService messageService;

    @Override
    public String getEvent() {
        return AppConstant.SOCKET_EVENT_MESSAGE;
    }

    @Override
    public Class<MsgPayload> getDataClass() {
        return MsgPayload.class;
    }

    @Override
    public void onMessage(SocketIOClient socketIOClient, MsgPayload data, AckRequest ackRequest) throws Exception {
        log.debug("收到客户端消息：{}", JsonUtil.toJSON(data));
        messageService.saveAndSendMsg(data);
    }

}
